package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commo.PageResult;
import com.itheima.commo.R;
import com.itheima.dto.DishDto;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.Orders;
import com.itheima.mapper.OrdersDetailMapper;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 订单管理控制层
 */
@RequestMapping("/order")
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 去下单
     * session:为了得到用户id 和 用户名 保存到orders订单表中
     */
    @PostMapping("/submit")
    public R<String> add(@RequestBody Orders orders, HttpSession session) {
        orderService.add(orders, session);
        return R.success("下单成功了");
    }


    /**
     * 订单明细
     */
    @GetMapping("/page")
    public R<PageResult> findPage(@RequestParam(value = "page", defaultValue = "1") Long page,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize) {
        PageResult<Orders> pageResult = orderService.findPage(page, pageSize);
        return R.success(pageResult);
    }

    /**
     * 派送订单
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders) {
        orderService.update(orders);
        return R.success("修改订单成功");
    }


    /**
     * 手机端查看历史订单
     */
    @GetMapping("userPage")
    public R<Page> userPage(Integer page, Integer pageSize) {
        return orderService.userPage(page, pageSize);
    }


    /**
     * 客户端再来一单
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        orderDetailService.again(orders);
        return R.success("增加成功");
    }
}
